package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.config.ShiroUtils;
import com.book.mapper.AuthorMapper;
import com.book.model.Author;
import com.book.result.Result;
import com.book.result.ResultEnum;
import com.book.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AuthorServiceImpl implements AuthorService {

    @Autowired
    private AuthorMapper authorMapper;

    @Override
    public Result AuthorRegister(String code, Object dx, Author author) {
        QueryWrapper<Author> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",author.getUsername());
        Author username = authorMapper.selectOne(queryWrapper);
        if(!Objects.equals(code,dx)){
            return Result.response(ResultEnum.CODE_SB);
        }else if(username!=null){
            return Result.response(ResultEnum.USERNAME_SB);
        }else if(author.getPassword()==null){
            return Result.response(ResultEnum.PASSWORD_NULL);
        }
        String s = ShiroUtils.generateSalt(8);
        String md5 = ShiroUtils.encryptPassword("md5", author.getPassword(), s, 7);
        author.setPassword(md5);
        author.setSalt(s);
        authorMapper.insert(author);
        return Result.response(ResultEnum.REGISTER_CG);
    }
}
